#coding:utf-8
import struct
import socket
import binascii
import sys

#a = 20 #0x14
#b = 400 #0x190
#str = struct.pack("ii", a, b)
#print repr(str)
#'\x14\x00\x00\x00\x90\x01\x00\x00'
#00000190,00000014
#str1 = struct.pack("!ii",a, b)
#print repr(str1)
#'\x00\x00\x00\x14\x00\x00\x01\x90'

ver = (4 << 4 | 20 / 4)
ver1 = struct.pack("!B",ver)
#print binascii.hexlify(ver1) #69=0x45=E

tos = 0
tos1 = struct.pack("!B", tos)
#print binascii.hexlify(tos1)

tot_len = 40
tot1 = struct.pack("!H",tot_len)
#print binascii.hexlify(tot1)
#(=40=0x28

id = 54321
id1 = struct.pack("!H", id)
#print binascii.hexlify(id1)

frag_off = 64
frag_off1 = struct.pack("H", frag_off)
#print binascii.hexlify(frag_off1)

ttl = 64
ttl1 = struct.pack("!B", ttl)
#print binascii.hexlify(ttl1)

proto = socket.IPPROTO_TCP
proto1 = struct.pack("!B", proto)
#print binascii.hexlify(proto1)

checksum = 0
checksum1 = struct.pack("!H", checksum)
#print binascii.hexlify(checksum1)

source_ip = "192.168.1.102"
sip = socket.inet_aton(source_ip)
sip1 = struct.pack("!4s", sip)
#print binascii.hexlify(sip1)

dest_ip = "192.168.1.100"
dip = socket.inet_aton(dest_ip)
dip1 = struct.pack("!4s", dip)
#print binascii.hexlify(dip1)

ip_header = struct.pack("!BBHH",ver,tos,tot_len,id)+struct.pack("H",frag_off)+struct.pack("!BBH4s4s",ttl,proto,checksum,sip,dip)
print binascii.hexlify(ip_header)

try:
	s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
except socket.error , msg:
	print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
	sys.exit()

#s.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)
#s.setsockopt(socket.SOL_SOCKET,socket.SO_SNDTIMEO,2000)

packet = ip_header
s.sendto(packet,(dest_ip, 0))
